﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Repositories;
using Estsh.Core.Wms.IRepositories;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Repositories
{
	public class APSInterfaceRepository : BaseRepository<BaseEntity>, IAPSInterfaceRepository
	{

		public APSInterfaceRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
		{

		}

		public WmsResponseResult GetAPSMenuList(int userId, int menuId)
		{
			using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
			{
				dbConn.Open();

				WmsResponseResult result= new WmsResponseResult();

				StringBuilder SqlStringBuilder = new StringBuilder(1024);

				SqlStringBuilder.Append(" SELECT * FROM dbo.sys_emp (nolock) WHERE enabled='Y' AND emp_id='" + userId + "' ");
				SysEmp empInfo = dbConn.Query<SysEmp>(SqlStringBuilder.ToString()).First();

				return result;
			}


		}


		/// <summary>
		/// 根据登录账号获取Main页主菜单列表
		/// </summary>
		/// <param name="empId"></param>
		/// <returns></returns>
		public List<SysWebMenu> getMainMenuListByUser(int userId, int menuId)
		{
			using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
			{
				if (dbConn.State != ConnectionState.Open)
				{
					dbConn.Open();
				}
				StringBuilder SqlStringBuilder = new StringBuilder(1024);
				SqlStringBuilder.Append("SELECT  m.* ");
				SqlStringBuilder.Append("FROM    sys_web_menu m ");
				SqlStringBuilder.Append("        JOIN sys_role_privliege rp ON m.menu_id = rp.fun_name ");
				SqlStringBuilder.Append("        AND rp.program = 'web' ");
				SqlStringBuilder.Append("        JOIN sys_role_emp re ON rp.role_id = re.role_id ");
				SqlStringBuilder.Append("WHERE   re.emp_id = '"+ userId + "' ");
				SqlStringBuilder.Append("        AND m.enabled = 'Y' ");
				SqlStringBuilder.Append("        AND m.parent_id = '"+ menuId + "' ");
				SqlStringBuilder.Append("ORDER BY m.sort_num ");

				List<SysWebMenu> result = dbConn.Query<SysWebMenu>(SqlStringBuilder.ToString()).ToList();

				return result;
			}
		}

        /// <summary>
        /// 根据登录账号获取按钮
        /// </summary>
        /// <param name="empId"></param>
        /// <returns></returns>
        public List<SysProgramFunOp_Extend> getProgramFunOpListByUser(int userId, int menuId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                if (dbConn.State != ConnectionState.Open)
                {
                    dbConn.Open();
                }
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("SELECT o.* FROM dbo.sys_program_fun_op o  ");
                SqlStringBuilder.Append(" JOIN sys_role_op_privliege pp ON o.ruid = pp.op_id  ");
                SqlStringBuilder.Append(" JOIN sys_role_emp re ON pp.role_id = re.role_id WHERE   re.emp_id = '"+ userId + "'   ");
                SqlStringBuilder.Append(" AND o.enabled = 'Y'  AND o.fun_name='"+ menuId + "' ORDER BY o.sort_num ");
               
                List<SysProgramFunOp_Extend> result = dbConn.Query<SysProgramFunOp_Extend>(SqlStringBuilder.ToString()).ToList();

                return result;
            }
        }

        /// <summary>
        /// 根据登录人，查询信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        //public SysEmp getEmp(int userId)
        //{
        //	using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
        //	{
        //		dbConn.Open();
        //		StringBuilder SqlStringBuilder = new StringBuilder(1024);
        //		SqlStringBuilder.Append(" SELECT * FROM dbo.sys_emp (nolock) WHERE enabled='Y' AND emp_id='"+ userId + "' ");
        //		SysEmp result = dbConn.Query<SysEmp>(SqlStringBuilder.ToString()).First();
        //		return result;
        //	}


        //}








    }
}
